<?
session_start();
require_once 'connect.php';
require_once 'PHPMailer/class.phpmailer.php';

function get_jobs(){
	return array(
		 1 => "Hand-to-Hand"
		, 2 => "Sword"
		, 3 => "Axe"	
		, 4 => "Archery"		
		, 5 => "Polearm"			
		, 6 => "Shield"			
		, 7 => "Thaumaturgy"
		, 8 => "Conjury"
		, 9 => "Carpenter"
		, 10 => "Blacksmith"
		, 12 => "Armorsmith"
		, 13 => "Goldsmith"
		, 14 => "Leatherworker"
		, 15 => "Weaver"
		, 16 => "Alchemist"
		, 17 => "Culinarian"
		, 18 => "Miner"
		, 19 => "Botanist"
		, 20 => "Fisher"
		);
}

function get_options() {
	return array(
		1 => 'Not so great',
		2 => 'Quite good',
		3 => 'Good',
		4 => 'Great!',
		5 => 'Excellent!',
	);
}

function getBaseUrl(){
	return "http://www.ffxivspecs.com";
}

function saveImage($encodedPNGData, $percent, $specId){

	$binaryData = base64_decode($encodedPNGData);
	$file = "../media/specImages/".$specId.".png";
	file_put_contents($file, $binaryData);
	

	/* Get new sizes
	list($width, $height) = getimagesize($file);
	$newwidth = $width * $percent;
	$newheight = $height * $percent;

	// Load
	$thumb = imagecreatetruecolor($newwidth, $newheight);
	$source = imagecreatefrompng($file);

	// Resize
	imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

	// Output
	imagejpeg($thumb, "../media/specImages/".$specId."_small.png");*/
}

function validate(){ 

	$sess_name = session_name();

	if(isset($_SESSION['password']) && isset($_SESSION['email'])){
		$password = mysql_real_escape_string($_SESSION['password']);
		$email = mysql_real_escape_string($_SESSION['email']);
		
	}elseif(isset($_COOKIE["ffxivspecs"])){
		$password = mysql_real_escape_string($_COOKIE["ffxivspecs[password]"]);
		$email = mysql_real_escape_string($_COOKIE["ffxivspecs[email]"]);
	}else{
		return 0; 
	}
	return checkUser($email, $password, 1);

}

function checkUser($em, $pw, $isMd5){

	if (isset($em) && isset($pw)){
	
		if($isMd5 == 1){
			$passw = $pw;
		}else{
			$passw = md5($em.$pw);
		}
		$abfrage = "SELECT userId
				   ,userName
				   ,email
				   ,password
				   ,avatar
				   ,picture
				   ,signature
				   ,description
				   ,gameCharacter
				   ,gameServer
				   ,posts
				   ,cdate
				   ,udate
				   ,notification
			FROM ffxivc_users
			WHERE email =\"".$em."\"
			AND password =\"".$passw."\"";

		$res = mysql_query($abfrage);
		if ($res){
			$num = mysql_num_rows($res);
			if($num == 1){
				$sess_name = session_name();
				$_SESSION['userId'] = mysql_result($res, 0, "userId"); 
				setcookie("ffxivspecs[userId]", mysql_result($res, 0, "userId"),time()+3600*24*7, "/", ".ffxivspecs.com", 1);
				$_SESSION['userName'] = mysql_result($res, 0, "userName");
				setcookie("ffxivspecs[userName]", mysql_result($res, 0, "userName"),time()+3600*24*7,  "/", ".ffxivspecs.com", 1);
				$_SESSION['email'] = mysql_result($res, 0, "email");
				setcookie("ffxivspecs[email]", mysql_result($res, 0, "email"),time()+3600*24*7,  "/", ".ffxivspecs.com", 1);
				$_SESSION['password'] = mysql_result($res, 0, "password");
				setcookie("ffxivspecs[password]", mysql_result($res, 0, "password"),time()+3600*24*7,  "/", ".ffxivspecs.com", 1);
				$_SESSION['avatar'] = mysql_result($res, 0, "avatar");
				setcookie("ffxivspecs[avatar]", mysql_result($res, 0, "avatar"),time()+3600*24*7,  "/", ".ffxivspecs.com", 1);
				$_SESSION['picture'] = mysql_result($res, 0, "picture");
				setcookie("ffxivspecs[picture]", mysql_result($res, 0, "picture"),time()+3600*24*7,  "/", ".ffxivspecs.com", 1);
				$_SESSION['signature'] = mysql_result($res, 0, "signature");
				setcookie("ffxivspecs[signature]", mysql_result($res, 0, "signature"),time()+3600*24*7,  "/", ".ffxivspecs.com", 1);
				$_SESSION['description'] = mysql_result($res, 0, "description");
				setcookie("ffxivspecs[description]", mysql_result($res, 0, "description"),time()+3600*24*7,  "/", ".ffxivspecs.com", 1);
				$_SESSION['gameCharacter'] = mysql_result($res, 0, "gameCharacter");
				setcookie("ffxivspecs[gameCharacter]", mysql_result($res, 0, "gameCharacter"),time()+3600*24*7, "/" , ".ffxivspecs.com", 1);
				$_SESSION['gameServer'] = mysql_result($res, 0, "gameServer");
				setcookie("ffxivspecs[gameServer]", mysql_result($res, 0, "gameServer"),time()+3600*24*7,  "/", ".ffxivspecs.com", 1);
				$_SESSION['posts'] = mysql_result($res, 0, "posts");
				setcookie("ffxivspecs[posts]", mysql_result($res, 0, "posts"),time()+3600*24*7, "/" , ".ffxivspecs.com", 1);
				$_SESSION['cdate'] = mysql_result($res, 0, "cdate");
				setcookie("ffxivspecs[cdate]", mysql_result($res, 0, "cdate"),time()+3600*24*7,  "/", ".ffxivspecs.com", 1);
				$_SESSION['udate'] = mysql_result($res, 0, "udate");
				$_SESSION['notification'] = mysql_result($res, 0, "notification");
				setcookie("ffxivspecs[udate]", mysql_result($res, 0, "udate"),time()+3600*24*7,  "/", ".ffxivspecs.com", 1);
				setcookie("ffxivspecs[notification]", mysql_result($res, 0, "notification"),time()+3600*24*7,  "/", ".ffxivspecs.com", 1);
				return 1;
			}
		}
	}
	return 0;
}

function logoutUser(){

	$_SESSION = array();

	if (isset($_COOKIE["ffxivspecs"])) {
		setcookie("ffxivspecs", '', time()-42000, '/');
	}

	session_destroy();
	return 1;
}

function getNewMessages(){
	
	$abfrage = "SELECT count(messageId) as newMessages FROM ffxivc_messages msg
				WHERE 
					NOT EXISTS ( SELECT memoryId FROM ffxivc_memory WHERE type = 'MSG' AND parentId = msg.messageId AND userId = \"".$_SESSION['userId']."\" )
					AND (msg.toId = \"".$_SESSION['userId']."\" OR msg.fromId = \"".$_SESSION['userId']."\")
					AND msg.parentId = 0";
	
	$res = mysql_query($abfrage);
	if($res){
		return mysql_result($res, 0, "newMessages");
	}else{
		return 0;
	}

}
function sendMail($toMail, $toName, $fromName, $mes, $action){

	$mail = new PHPMailer(false);
	try {
		$mail->AddReplyTo('ffxivspecs@gmail.com', 'FFXIVSpecs');
		$mail->AddAddress($toMail, $toName);
		$mail->SetFrom('ffxivspecs@gmail.com', 'FFXIVSpecs');
		$mail->AddReplyTo('ffxivspecs@gmail.com', 'FFXIVSpecs');
		$mail->Subject = '[FFXIVSpecs] Notification';
		$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
		$url = getBaseUrl();
		$mail->MsgHTML(file_get_contents($url."/php/send_email.php?action=$action&comment=".urlencode($mes)."&sender=".urlencode($fromName)));
		$mail->Send();
		return 1;
	} catch (phpmailerException $e) {
	  echo $e->errorMessage(); //Pretty error messages from PHPMailer
	} catch (Exception $e) {
	  echo $e->getMessage(); //Boring error messages from anything else!
	}
	return 0;

}


?>